<?php
declare (strict_types=1);

namespace app\admin\controller;


use app\common\facade\Builder;

use think\App;
use think\facade\View;
use think\response\Json;

class Curriculum extends Base
{

    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\Curriculum';

    //验证器
    protected string $validate = 'Curriculum';

    //关联模型
    protected array $hasOne = ['cate1'];

    public function index(): string
    {
        //构建table
        return Builder::Table()
            ->addColumn(['type' => 'checkbox', 'fixed' => 'left', 'field' => '', 'title' => '全选'])
            ->addColumn(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addColumn(['field' => 'title', 'title' => '课程名称', 'modify' => '2,input'])
            ->addColumn(['field' => 'cate_1', 'title' => '课程分类', 'templet' => '<div>{{d.cate1.title}}</div>', 'modify' => '2,select,getCate'])
            ->addColumn(['field' => 'teacher', 'title' => '教师', 'modify' => '2,input'])
            ->addColumn(['field' => 'introduction', 'title' => '简介', 'modify' => '2,input'])
            ->addColumn(['field' => 'price', 'title' => '价格', 'modify' => '2,input'])
            ->addColumn(['field' => 'frequency', 'title' => '播放次数', 'modify' => '2,input'])
            ->addColumn(['field' => 'images', 'title' => '缩略图', 'modify' => '2,img,1', 'templet' => '#imagesTpl'])
            ->addColumn(['field' => 'content', 'title' => '详情', 'modify' => '2,rich_text', 'templet' => '#editorTpl'])
            ->addColumn(['field' => 'sort', 'title' => '排序', 'modify' => '2,input', 'templet' => '#sort_sort', 'width' => 120])
            ->addColumn(['field' => 'create_time', 'title' => '创建时间'])
            ->addColumn(['field' => 'update_time', 'title' => '更新时间'])
            ->addColumn(['fixed' => 'right','field' => 'status', 'title' => '上架状态', 'templet' => '#status', 'width' => 120])
            ->addColumn(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 200])
            ->addTopButtons(['add' => ['size' => '1200,1200'], 'deleteAll'])
            ->addRightButtons(['edit' => ['size' => '1200,1200'], 'deliver' => ['title' => '编辑章节', 'size' => '1200,1200', 'url' => url('chapter/index')->build(), 'event' => '_openNewPage'], 'delete'])
            ->addSwitchTool([
                ['name' => 'status', 'pk' => 'id', 'text' => '开启|关闭'],
            ])
            ->addSortTool([['name' => 'sort', 'pk' => $this->pk, 'url' => url('update')->build()]])
            ->addSearch([
                ['title', '输入课程名称 ', 'input', '', '', '', 'like'],
            ])
            ->fetch();
    }


    public function update(): Json
    {
        try {
            $param = $this->request->post();


            return json($this->model::_update($param, $this->getLogsArguments($param)));
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }


    public static function getCate(): array
    {
        return \app\common\model\CurriculumCate::where(['is_del' => 0])->column('title', 'id');
    }

    public function add($tpl = 'curriculum/edit'): string
    {
        View::assign('form', ['title' => '课程图片', 'field' => 'images', 'value' => 5]);
        return parent::add($tpl); // TODO: Change the autogenerated stub
    }

    public function edit($id = '', $tpl = 'curriculum/edit', $info = ''): string
    {
        View::assign('form', ['title' => '课程图片', 'field' => 'images', 'value' => 5]);
        return parent::edit($id, $tpl, $info); // TODO: Change the autogenerated stub
    }


    public function save($param = [], $model = false): Json
    {
        try {
            $param = $this->request->post();
//            dump($param);die;
            if(isset($param['images']) && $param['images']){
                $param['icon'] = explode(',',$param['images'])[0];
            }
            return parent::save($param, $model); // TODO: Change the autogenerated stub
        } catch (\Exception $e) {
            dump($e->getMessage());die;

        }
    }


}
